package com.example.demo.com.java8.forkjoin;

import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

/**
 * @author chengfei5
 * @date 2018/8/21
 */
public class ForkJoinTest {
    @Test
    public void test(){
        Instant start = Instant.now();
        ForkJoinPool pool = new ForkJoinPool();
        ForkJoinTask<Long> task = new ForkJoinCalculate(0,10000000000L);
        Long sum = pool.invoke(task);
        System.out.println(sum);
        Instant end = Instant.now();

        System.out.println("耗费时间为："+Duration.between(start,end).toMillis());
    }

    /**
     *
     */
    @Test
    public void test2(){
        Instant start = Instant.now();
        long sum = 0L;
        for(long i = 0;i<=10000000000L;i++){
            sum +=i;
        }
        System.out.println(sum);
        Instant end = Instant.now();

        System.out.println("耗费时间为："+Duration.between(start,end).toMillis());
    }
    @Test
    public void test3(){
        long reduce = LongStream.rangeClosed(0, 10000000L)
                //并行流
                .parallel()
                .sum();
        System.out.println(reduce);
    }
}
